public class Test {
    public static void main(String[] args) {
        //
        int[] nums = {8828,9581,49,9818,9974,9869,9991,10000,10000,10000,9999,9993,9904,8819,1231,6309};
        Solution solution = new Solution();
        int ret = solution.minOperations(nums,134365);
        System.out.println(ret);
    }
}

class Solution {
    public int minOperations(int[] nums, int x) {
        //
        int left = 0,right = 0;
        int sum = 0;//窗口内的和
        int ret = 0;//
        int count = 0;//窗口内的元素个数

        int sum_nums = 0;//整个数组的和
        for(int num:nums){
            sum_nums += num;
        }

        if(sum_nums<x){
            return -2;
        }else if(sum_nums==x){
            return nums.length;
        }

        for(right = 0;right<nums.length;right++){
            sum += nums[right];//进窗口   right指向的    意义是，sum中加上了right指向的元素

            while(sum>sum_nums-x){
                sum -= nums[left];//出窗口
                left++;
            }
            if(sum==sum_nums-x){
                count = Math.max(count,right-left+1);//更新结果
            }
        }


        if(count ==0 ){
            return -1;
        }


        ret = nums.length-count;
        return ret;


    }
}
